/**
 * Created by wry on 2015/2/12.
 */
'use strict';

angular.module('backofficeApp.retailerManager.controllers', []).
    controller('retailerCtrl', ['$scope', '$http', '$location', 'CommunityService', function ($scope, $http, $location, communityService) {

        function datePicker(id) {
            function d(id) {
                var self = this;
                $(id).datepicker({
                    format: 'yyyy-mm-dd',
                    autoclose: true,
                    forceParse: false
                }).on('changeDate', function (e) {
                    self.date = e.date;
                }).on('show', function (e) {
                    $(".datepicker.datepicker-dropdown.dropdown-menu").css("z-index", "10000");
                });
            }

            d.prototype.getValue = function () {
                return this.date.setHours(0, 0, 0, 0)
            }
            var date = new d(id);
            return date;
        }

        $scope.cDate = datePicker('#cooperationDate');
        $scope.getRetailerList = function (parentId) {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getRetailerList/' + parentId)
                .success(function (data) {
                    $scope.retailerList = data.data;
                }).error(function () {
                    $scope.retailerList = null;
                });
        };
        $scope.getRetailerList(0);

        $scope.addRetailer = function () {
            $scope.retailer = null;
            $('#cooperationDate').val('');
            $('#zong_model').modal('show');
        };

        $scope.saveRetailer = function (retailer) {

            if (!retailer || !retailer.retailer_name) {
                return;
            }
            retailer.parent_id = 0;
            retailer.starting_time = $scope.cDate.getValue();

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/addMainRetailer', retailer, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $('#zong_model').modal('hide');
                $scope.getRetailerList(0)
            }).error(function () {
            });
        };

        $scope.gotoRetailerBranch = function (id) {
            $location.path('/retailer/' + id)
        };

        $scope.editRetailer = function (retailer) {
            $scope.retailer = retailer;
            $scope.codeErrMsg = null;
            $('#cooperationDate').val('');
            $('#zong_model').modal('show');
        };

// 删除操作
        $scope.deleteMainRetailer = function (id) {

            var opt = {id: id};

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/deleteMainRetailer', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.retailerList = data.data;
                $scope.getRetailerList(0);
            }).error(function () {
            });
        };



        $scope.searchRetailer = function (name) {
            var opt = {
                parentId: 0,
                name: name
            };

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/searchRetailerByName', opt, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {
                $scope.retailerList = data.data;
            }).error(function () {
            });
        }

    }]).
    controller('branchRetailerCtrl', ['$scope', '$http', '$location', '$routeParams', function ($scope, $http, $location, $routeParams) {

        var id = $routeParams.id;

        function datePicker(id) {
            function d(id) {
                var self = this;
                $(id).datepicker({
                    format: 'yyyy-mm-dd',
                    autoclose: true,
                    forceParse: false
                }).on('changeDate', function (e) {
                    self.date = e.date;
                }).on('show', function (e) {
                    $(".datepicker.datepicker-dropdown.dropdown-menu").css("z-index", "10000");
                });
            }

            d.prototype.getValue = function () {
                if (this.date == null) {
                    return null;
                }
                return this.date.setHours(0, 0, 0, 0)
            }
            var date = new d(id);
            return date;
        }

        $scope.cDate = datePicker('#cooperationDate');
        $scope.getRetailerList = function (parentId) {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getRetailerList/' + parentId)
                .success(function (data) {
                    $scope.retailerList = data.data;

                    $scope.retailerList.forEach(function (r) {
                        if (r.retailer_icon != null) {
                            r.retailer_icon = fastDfsHostIp + r.retailer_icon;
                        }
                    })
                }).error(function () {
                    $scope.retailerList = null;
                });
        };
        $scope.getRetailerList(id);

        $scope.addBranchRetailer = function () {
            $scope.retailer = null;
            $scope.codeErrMsg = null;
            $('#cooperationDate').val('');
            $('#d_code').removeClass("has-error");
            $('#branch_model').modal('show');
        };

        $scope.selectFile = function (files) {
            $scope.image = files[0]
        };


        $scope.saveRetailerBranch = function (retailer) {

            if (!retailer || !retailer.retailer_name || !retailer.code || !retailer.address || !retailer.contact_name || !retailer.phone) {
                return;
            }

            var fd = new FormData();
            if ($scope.image != null)
                fd.append('image1', $scope.image);
            if (retailer.id != null)
                fd.append('id', retailer.id);
            fd.append('parent_id', Number(id));
            fd.append('code', retailer.code);
            fd.append('retailer_name', retailer.retailer_name);
            fd.append('address', retailer.address);
            fd.append('contact_name', retailer.contact_name);
            fd.append('phone', retailer.phone);
            if ($scope.cDate.getValue() != null) {
                fd.append('starting_time', $scope.cDate.getValue());
            } else {
                fd.append('starting_time', retailer.starting_time);
            }


            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/addRetailer', fd, {
                transformRequest: angular.identity,
                headers: {'Content-Type': undefined}
            }).success(function (data) {
                if (data.result.status == 1) {
                    $scope.getRetailerList(id);
                    $('#branch_model').modal('hide');
                } else {
                    $scope.codeErrMsg = '分店编码不能重复';
                    $('#d_code').addClass("has-error");
                    $scope.getRetailerList(id);
                }
            }).error(function () {
            });
        };

        $scope.editRetailer = function (retailer) {
            $scope.retailer = retailer;
            $scope.codeErrMsg = null;
            $('#d_code').removeClass("has-error");
            $('#branch_model').modal('show');
        };

        $scope.backRetailer = function () {
            $location.path('/retailer')
        };

        $scope.cancelSave = function () {
            $scope.getRetailerList(id);
        };

        $scope.gotoBranchAgreement = function (id) {
            $location.path('/agreement/' + id)
        }
    }]).
    controller('agreementCtrl', ['$scope', '$http', '$location', '$routeParams', function ($scope, $http, $location, $routeParams) {

        var branchId = $routeParams.id;

        $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getAgreementVoucher')
            .success(function (data) {
                $scope.voucherType = data.data;
            })

        $scope.getAgreementList = function (branchId) {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/getRetailerAgreement/' + branchId)
                .success(function (data) {
                    $scope.agreementList = data.data;
                })
        };
        $scope.getAgreementList(branchId);
        $scope.addAgreement = function () {
            $('#typeEdit').attr("disabled", false);
            $scope.agreement = null;
            $scope.typeErrMsg = null;
            $('#d_code').removeClass("has-error");
            $('#agree_model').modal('show')
        };

        $scope.editAgreement = function (agreement) {
            $('#typeEdit').attr("disabled", true);
            $scope.agreement = agreement;
            $scope.typeErrMsg = null;
            $('#d_code').removeClass("has-error");
            $('#agree_model').modal('show');
        };

        $scope.cancelSave = function () {
            $scope.getAgreementList(branchId);
        };
        $scope.deleteAgreement = function (id) {
            $http.get('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/deleteAgreement/' + id)
                .success(function (data) {
                    $scope.getAgreementList(branchId);
                })
        };

        $scope.saveAgreement = function (agreement) {
            if (!agreement || !agreement.voucher_type_id || !agreement.deal_price || !agreement.retailer_price) {
                return;
            }
            var data = {
                voucher_type_id: agreement.voucher_type_id,
                retailer_id: Number(branchId),
                deal_price: Number(agreement.deal_price),
                retailer_price: Number(agreement.retailer_price)
            };
            if (agreement.id != null) {
                data.id = agreement.id;
            }

            $http.post('http://' + backOfficeHost + '/rest/v1.0/backoffice/voucher/addRetailerAgreement', data, {
                headers: {'Content-Type': 'application/json;charset=utf-8'}
            }).success(function (data) {

                if (data.result.status == 1) {
                    $('#agree_model').modal('hide');
                    $scope.getAgreementList(branchId)
                } else {
                    $scope.typeErrMsg = '此协议已经添加';
                    $('#d_code').addClass("has-error");
                    $scope.getRetailerList(id);
                }
            }).error(function () {
            });
        }

    }]);

